//
//  FunctionVC.m
//  SuperLight
//
//  Created by Charlie Liao on 14-4-30.
//  Copyright (c) 2014年 Charlie Liao. All rights reserved.
//

#import "FunctionVC.h"

@interface FunctionVC ()
{
    int selectedSegmentIndex;
    int selectedEffectsRow;
}

@end


@implementation FunctionVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        effectsArr = @[@"RGB tune timer", @"Color Change smooth random", @"RGB blink", @"RGB tune timer ON Mic", @"Color Change smooth random ON Mic"];
        selectArr = [NSMutableArray array];
        selectedEffectsRow = -1;
        selectedSegmentIndex = -1;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupNavigation];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    for (LightEntity *entity in [Common share].lightsArr) {
        if (entity.isSelect) {
            [selectArr addObject:entity];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 * 设置导航栏
 */
- (void)setupNavigation
{
    self.navigationController.navigationBar.translucent = YES;
    self.title = @"Function";
}

#pragma mark  UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return effectsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"functioncell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"functioncell"];
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
        cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
        cell.textLabel.textColor = RGBColor(50, 120, 249);
    }
    
    if (selectedEffectsRow == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    cell.textLabel.text = effectsArr[indexPath.row];
    
    return cell;
}

#pragma mark  UITabBarDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    for (UITableViewCell* cell in  tableView.visibleCells) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    selectedEffectsRow = indexPath.row;
    [self setEffects:selectedEffectsRow];
    
}

- (void)setEffects:(int)mode
{
    switch (mode) {
        case 0:
            for (LightEntity* entity in selectArr) {
                [[LightController shareInstance] startRGBTurn:self.timeIntervalSlider.value brightness:self.brightness addr:entity.addr];
            }
            break;
            
        case 1:
            for (LightEntity* entity in selectArr) {
                [[LightController shareInstance] startColorSmooth:self.timeIntervalSlider.value brightness:self.brightness addr:entity.addr];
            }
            break;
        case 2:
            for (LightEntity* entity in selectArr) {
                [[LightController shareInstance] startRGBBlink:self.timeIntervalSlider.value brightness:self.brightness addr:entity.addr];
            }
            break;
            
        case 3:
            for (LightEntity* entity in selectArr) {
                [[LightController shareInstance] startRGBTurnOnMic:self.timeIntervalSlider.value brightness:self.brightness addr:entity.addr];
            }
            break;
            
        case 4:
            for (LightEntity* entity in selectArr) {
                [[LightController shareInstance] startColorSmoothOnMic:self.timeIntervalSlider.value brightness:self.brightness addr:entity.addr];
            }
            break;
            
        default:
            break;
    }
}

#pragma mark  Action

- (void)onClickDoneBtn
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)timeIntervalValueChanged:(id)sender
{
    [self setEffects:selectedEffectsRow];
}

- (IBAction)timingPowerValueChanged:(id)sender
{
    selectedSegmentIndex = self.timingSegmented.selectedSegmentIndex;
    [[UIApplication sharedApplication].keyWindow addSubview:self.dateChoseBgView];
    CGRect f = self.dateChoseView.bounds;
    self.dateChoseView.bounds = CGRectZero;
    self.dateChoseBgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    self.datePicker.minimumDate = [NSDate date];
    self.datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:65535*60];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.dateChoseBgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
        self.dateChoseView.bounds = f;
    }];
}

- (IBAction)onClickHideDateChoseView:(id)sender
{
    [self.dateChoseBgView removeFromSuperview];
}

- (IBAction)onClickConfirmBtn:(id)sender
{
    [self.dateChoseBgView removeFromSuperview];
    
    int timing = 0;
    
    double interval = self.datePicker.date.timeIntervalSinceNow;
    
    if (20 < interval && interval < 60) {
        timing = 1;
    } else {
       timing = interval / 60;
    }

    
    for (LightEntity* entity in selectArr) {
        if (selectedSegmentIndex == 0) {
            [[LightController shareInstance] setPowerOnAfter:timing addr:entity.addr];
            selectedEffectsRow = -1;
            [self.effectsTable reloadData];
        } else if(selectedSegmentIndex == 1) {
            [[LightController shareInstance] setPowerOffAfter:timing addr:entity.addr];
        }
    }
}

- (IBAction)datePickerValueChanged:(id)sender
{
    
}

@end
